<template>
  <div class="fileSys">
    <el-table
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      :data="tableData"
      stripe
      :header-cell-style="{
          background:'#E0E0E0'
        }"
      style="width: 100%"
      class="tableData-box"
    >
      <el-table-column type="index" width="50" label="序号"> </el-table-column>
      <template v-for="item in tableColumns">
        <el-table-column
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
          align="center"
          v-if="item.isShow"
          :key="item.prop"
        >
          <template v-slot="scope">
            <template v-if="item.format == 'progress'">
              <el-progress
                :percentage="
                  Number(calcCheng(scope.row[item.prop], 100).toFixed(1))
                "
                :color="customColorMethod"
              >
              </el-progress>
            </template>
            <template v-else-if="item.format == 'GB'">
              <div>
                {{
                  scope.row[item.prop]
                    ? scope.row[item.prop] + "&nbsp;GB"
                    : scope.row[item.prop]
                }}
              </div>
            </template>
            <template v-else>
              <div>
                {{ scope.row[item.prop] }}
              </div>
            </template>
          </template>
        </el-table-column>
      </template>
    </el-table>
  </div>
</template>

<script>
import { fetchData } from "@/api/index.js";
import tableColumns from "./FileSystemColumns.js";
import bus from "common/bus.js";
import { GetItem } from "@/utils/dataStore";
export default {
  data() {
    return {
      postData: {
        opera: "fileSystem",
        curPage: 1,
        pageSize: 20,
        sort: {
          sortColumn: "",
          sortOrders: "",
        },
        filtersType: "exactquery",
        filters: {},
      },
      tableData: [],
      dataTotal: 0,
      tableColumns: tableColumns,
      SearchIPArr: [],
      searchJson: {},
      SearchIP: "",
      timerName: "MonitorInfoServerFileSytem",
      timerEventName: "timerMonitorInfoServerFileSytem",
      timerInterval: 1000 * 60 * 5,
      loading: true,
    };
  },
  created() {
    GetItem(sessionStorage.getItem('PushId')).then(_ => {
      this.selectedMonID =_[0].articleAddress;
      this.postData.filters.ip =this.selectedMonID.IP ;
      this.getTableData();
    })

    bus.$emit("timerNewTodo", {
      name: this.timerName,
      eventName: this.timerEventName,
      interval: this.timerInterval,
      intervalNow: 0,
    });

    bus.$on(this.timerEventName, (msg) => {
      if (msg.isGetData) {
        this.getTableData();
      }
    });
  },
  mounted() {
    bus.$on("ItemMonitorServer", (msg) => {
      this.resetData();
      this.selectedMonID = msg;
      this.postData.filters.ip = this.selectedMonID.IP;

      this.getTableData();
    });
  },
  methods: {
    customColorMethod(percentage) {
      if (percentage <= 50) {
        return "#67c23a";
      } else if (percentage <= 75) {
        return "#e6a23c";
      } else {
        return "#f56c6c";
      }
    },
    getTableData() {
      this.loading = true;

      fetchData({ postData: JSON.stringify(this.postData) }).then((res) => {
        this.dataTotal = res.dataTotal;
        this.tableData = res.data;

        this.loading = false;
      });
    },
    resetData() {
      this.dataTotal = 0;
      this.tableData = [];
    },
  },
  watch: {},
  beforeDestroy() {
    // 页面关闭前，移除定时器任务
    bus.$emit("timerNewTodo", {
      name: this.timerName,
    });
  },
};
</script>

<style lang="scss" scoped>
.fileSys /deep/  .el-table thead {
  color: black;
}
.tableData-box{
    height: calc(100% - 30px);
}
.tableData-box > .el-col {
    height: 100%;
}
.tableData-box /deep/ .el-table__body-wrapper {
  overflow-y: scroll;
  height: 270px;
}
.tableData-box /deep/ .el-table__body-wrapper::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}
.tableData-box /deep/ .el-table__body-wrapper::-webkit-scrollbar-thumb {
    border-radius: 3px;
    background-color: #E0E0E0;
}
</style>
